package cn.zebra.dev.ibatis.plus.service.impl;

import cn.zebra.dev.common.utils.StrUtils;
import cn.zebra.dev.ibatis.plus.service.IService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;

import java.util.List;

/**
 * 简化版写法
 *
 * @author runnable@sina.cn
 */
public class ServiceImpl<M extends BaseMapper<T>, T> extends com.baomidou.mybatisplus.extension.service.impl.ServiceImpl<M, T> implements IService<T> {

    private QueryWrapper<T> wrapper;

    @Override
    public List<T> selectList() {
        wrapper = new QueryWrapper<>();
        return list(wrapper);
    }

    /**
     * 更新记录
     *
     * @param entity 实体对象
     * @param sqlAnd 条件拼接
     * @param params 拼接参数
     * @return 成功返回true, 失败返回false
     */
    @Override
    public boolean update(T entity, String sqlAnd, Object... params) {
        wrapper = new QueryWrapper<>();
        wrapper.apply(sqlAnd, params);
        return update(entity, wrapper);
    }

    /**
     * 查询全部记录
     *
     * @param setSelect 查询列
     * @param sqlAnd    条件拼接
     * @param params    拼接参数
     * @return 查询记录
     */
    @Override
    public List<T> selectList(String setSelect, String sqlAnd, Object... params) {
        wrapper = new QueryWrapper<>();
        if (StrUtils.isNotBlank(setSelect)) {
            wrapper.select(setSelect);
        }
        wrapper.apply(sqlAnd, params);
        return list(wrapper);
    }

    /**
     * 查询全部记录
     *
     * @param sqlAnd 条件拼接
     * @param params 拼接参数
     * @return 查询记录
     */
    @Override
    public List<T> selectList(String sqlAnd, Object... params) {
        wrapper = new QueryWrapper<>();
        wrapper.apply(sqlAnd, params);
        return list(wrapper);
    }

    /**
     * 查询全部记录
     *
     * @param column 列名
     * @param idList id List
     * @return 查询记录
     */
    @Override
    public List<T> selectBatch(String column, List<Long> idList) {
        wrapper = new QueryWrapper<>();
        wrapper.in(column, idList);
        return list(wrapper);
    }

    @Override
    public T selectOne(Long id) {
        wrapper = new QueryWrapper<>();
        wrapper.apply("id = {0}", id);
        wrapper.last("LIMIT 1");
        return getOne(wrapper);
    }

    @Override
    public T selectOne(String sqlAnd, Object... params) {
        wrapper = new QueryWrapper<>();
        wrapper.apply(sqlAnd, params);
        wrapper.last("LIMIT 1");
        return getOne(wrapper);
    }

    @Override
    public Integer count(String sqlAnd, Object... params) {
        wrapper = new QueryWrapper<>();
        wrapper.apply(sqlAnd, params);
        return count(wrapper);
    }

    @Override
    public boolean delete(String sqlAnd, Object... params) {
        wrapper = new QueryWrapper<>();
        wrapper.apply(sqlAnd, params);
        return remove(wrapper);
    }

    /**
     * 根据主键id查找对应记录
     *
     * @param id 主键id
     * @return 实体
     */
    @Override
    public T findById(Long id) {
        wrapper = new QueryWrapper<>();
        wrapper.apply("id = {0}", id);
        wrapper.last("LIMIT 1");
        return this.getOne(wrapper);
    }

    @Override
    public T findByFieldValue(SFunction<T, ?> field, Object value) {
        LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(field, value);
        wrapper.last("LIMIT 1");
        return this.getOne(wrapper);
    }

    /**
     * 根据字段和字段值匹配对象集合
     *
     * @param field 字段
     * @param value 字段值
     * @return 实体对象集合
     */
    @Override
    public List<T> findListByFieldValue(SFunction<T, ?> field, Object value) {
        LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(field, value);
        return this.list(wrapper);
    }

}
